$(document).ready(function() {	

	$('a[name=modal]').click(function(e) {
		e.preventDefault();
		
		var id = $(this).attr('href');
	
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		$('#mask').css({'width':maskWidth,'height':maskHeight});

		$('#mask').fadeIn(1000);	
		$('#mask').fadeTo("slow",0.8);	
	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
              
		$(id).css('top',  winH/2-$(id).height()/2);
		$(id).css('left', winW/2-$(id).width()/2);
	
		$(id).fadeIn(2000); 
	
	});
	
	$('.window .close').click(function (e) {
		e.preventDefault();
		
		$('#mask').hide();
		$('.window').hide();
	});		
	
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});			
	
});


//This method will validate and save the project
$(document).ready(function(){
	
	$('#submitPlayer').submit(function (e){
		
		if(validateDate()){
			
	      $.post('/jowey/project/save',$(this).serialize(), function(project) {
	    	  
	    	  $('#mask').hide();
	    	  $('.window').hide();
	    	  
	    	  var table = $('#projectTable');
	    	  var line		     = document.createElement("tr");
	    	  var tdCode    	 = document.createElement("td");
              var tdDescription	 = document.createElement("td");
          	  var tdBeginDate	 = document.createElement("td");
          	  var tdEndDate      = document.createElement("td");
          	  var tdActions      = document.createElement("td");
          	  
          	  tdCode.innerHTML        = project.idProject;
          	  tdDescription.innerHTML = project.projectDescription;
          	  tdBeginDate.innerHTML   = project.beginDate;
          	  tdEndDate.innerHTML     = project.endDate;
          	  tdActions.innerHTML     = "";
        	
          	  line.appendChild(tdCode);
           	  line.appendChild(tdDescription);
          	  line.appendChild(tdBeginDate);
          	  line.appendChild(tdEndDate);
          	  line.appendChild(tdActions);
          	  
          	   table.append(line);
          	  
          	   alert("Project " +project.projectDescription+" successfully saved");
	      });
	      
	      e.preventDefault();
	      clearInputs();
		}else{
			alert("Cant save");
		}   
	      
	});
});

function validateDate (){

	 var stringBeginDate = $('#inputBeginDate').val();
	   var stringEndDate = $('#inputEndDate').val();
	   
	   var year;
	   var month;
	   var day;
	  
	   year 	   = parseInt(stringBeginDate.substring(0,4));
	   month 	   = parseInt(stringBeginDate.substring(5,7));
	   day 	       = parseInt(stringBeginDate.substring(8,10));
	    
	   beginDate = new Date(year,month-1,day);
	   
	   year 	   = parseInt(stringEndDate.substring(0,4));
	   month 	   = parseInt(stringEndDate.substring(5,7));
	   day 	       = parseInt(stringEndDate.substring(8,10));
	   
	   endDate = new Date(year,month-1,day);
	   
	   if(endDate <= beginDate){
		   return false;
	   }else{
		   return true;
	   }
}

$(document).ready(function () {
    $.ajax({
        type:'GET',
        url:"/jowey/project/getProjects",
        dataType:'json',
        success:function (result) {
        	
        	var table = $('#projectTable');
        
        	var line;
        	var tdIdProject;
        	var tdProjectDescription; 		   
        	var tdBeginDate;   
        	var tdEndDate;
        	var tdAction;
        	
        	
        	for(var i = 0;i<result.length;i++){
  
        		line		          = document.createElement("tr");
        		tdIdProject 		  = document.createElement("td");
        		tdProjectDescription  = document.createElement("td");
        		tdBeginDate           = document.createElement("td");
        		tdEndDate	          = document.createElement("td");
            	tdAction              = document.createElement("td");
            	
            	
            	tdIdProject.innerHTML             = result[i].idProject;
            	tdProjectDescription.innerHTML 	  = result[i].projectDescription;
            	tdBeginDate.innerHTML             = result[i].beginDate;
            	tdEndDate.innerHTML		          = result[i].endDate;
            	tdAction.innerHTML                = "<a href='#'><img src='../resources/css/images/lapis.png'/></a>&nbsp;&nbsp;&nbsp;&nbsp;<a href='#'><img src='../resources/css/images/close.png'/></a>";
            	
            	line.appendChild(tdIdProject);
            	line.appendChild(tdProjectDescription);
            	line.appendChild(tdBeginDate);
            	line.appendChild(tdEndDate);
            	line.appendChild(tdAction);
            	
            	table.append(line);
            
        	}
        },
        error:function (jqXHR, textStatus, errorThrown) {
            alert("project " + textStatus + " " + errorThrown + " !");
        }
    });
});



function clearInputs() {
    $('input[id*="input"]').each(function () {
        $(this).val('');
    });
}